pkgname=pmu-tools
pkgver=git
pkgrel=1
pkgdesc="A collection of tools for profile collection and performance analysis on Intel CPUs on top of Linux perf. This uses performance counters in the CPU."
arch=('i386' 'x86_64')
url="https://github.com/andikleen/pmu-tools.git"
license=('GPL2' 'BSD')
makedepends_debian_11=(binutils-dev bison flex libaudit-dev libdw-dev libiberty-dev liblzma-dev libnuma-dev libunwind-dev zlib1g-dev python3 systemtap-sdt-dev libslang2-dev libgtk2.0-dev libperl-dev python3-dev libzstd-dev libcap-dev libbabeltrace-dev)
source=("https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git" "https://github.com/andikleen/pmu-tools.git")
md5sums=('SKIP' 'SKIP')

build_perf() {
    cd "${srcdir}/linux/tools/perf"
    PATH=$BUILD_DIR:$PATH make
}

build_pmu-tools() {
    cd "${srcdir}/pmu-tools"
    files=$(grep -rl "^#\!\/usr\/bin.*python" ./)
    for file in $files
    do
        sed -i 's/\/usr\/bin\/env\ python$/\/usr\/bin\/env\ python3/g' "$file"
        sed -i 's/\/usr\/bin\/python$/\/usr\/bin\/python3/g' "$file"
    done

    cd "${srcdir}/pmu-tools/jevents"
    make
}

build() {
    build_perf || return
    build_pmu-tools || return
}

package() {
    benchmark_path="${pkgdir}/lkp/benchmarks/${pkgname}"
    mkdir -p "${benchmark_path}"
    cd "${srcdir}/${pkgname}"
    XDG_CACHE_HOME="$benchmark_path" ./event_download.py -a
    cp -af "$srcdir/${pkgname}/"* "$benchmark_path"
    cp -af "$srcdir/linux/tools/perf/perf" "$benchmark_path"
}
